<?php

namespace app\index\controller;

use think\Controller;
use think\Db;

class Gl extends IndexController
{
    public function index()
    {
        $list = DB::table('business')
            ->paginate(5);


        return view('index/table-font-list', [
            'title' => '商家列表',
            'list' => $list
        ]);
    }

    public function edit($id)
    {
        $list = DB::table('business')
            ->where('id=' . $id)
            ->find();

        return view('index/edit', [
            'title' => '编辑商家信息',
            'list' => $list
        ]);
    }

    public function doedit()
    {
//        echo '111';
        $file = request()->post();
        $id = $file['id'];
        unset($file['id']);

//        dump($file);
        $data = DB::table('business')
            ->where('id', $id)
            ->update($file);

        if ($data) {
            $this->success('修改成功', 'index/gl/index');
        } else {
            $this->error('修改失败');
        }
    }

    public function create()
    {
        return view('index/add');
    }

    public function save()
    {
        $list = request()->post();

        if($list['name'] == ''){
            $this->error('商家名不能为空');
        }elseif($list['site'] == ''){
            $this->error('站点名不能为空');
        }elseif($list['dns'] == ''){
            $this->error('域名不能为空');
        }elseif($list['ptime'] == ''){
            $this->error('购买时长不能为空');
        }elseif($list['ip'] == ''){
            $this->error('ip不能为空');
        }elseif($list['price'] == ''){
            $this->error('金额不能为空');
        }

        $list['btime'] = strtotime($list['btime']);
//        dump($list);
//        die;
        $price = $list['price'];
        unset($list['price']);

        $data = DB::table('business')
                ->insertGetId($list);

        if ($data) {
            $pbus['price'] = $price;
            $pbus['bid']=$data;
            //插入
            $bus = DB('money')
                    ->insert($pbus);

            return $this->success('提交成功', url('index/gl/index'));
        } else {
            $this->error('提交失败');
        }
    }

    public function delete($id)
    {
        $data = Db::table('business')->where('id',$id)->delete();

        if($data){
            return true;
        }
    }

    public function img()
    {
        $list = DB::table('qqpt')
                    ->field('*')
                    ->select();
        return view('index/table-images-list',[
            'title' => '商家列表',
            'list' => $list
        ]);
    }


    public function select()
    {
        $bnn = input('post.bnn');
//            $bnn = '兄';

        if($bnn == '') {
            return view('index/gl');
        }else{
            $list = DB::table('business')
                ->where('name','like','%'.$bnn.'%')
                ->select();
            return $list;
        }
    }

    public function changeStatus($id)
    {
//        return true;
        $data = Db::table('business')
            ->field('status')
            ->where('id='.$id)
            ->find();

//        dump($data);
//        die;

        if($data){
            //如果有值
            if($data['status'] == 1){
               DB::table('business')
                    ->where('id',$id)
                    ->update([
                        'status'=>'0'
                    ]);
               $status = '0';
            }else{
               $status = DB::table('business')
                    ->where('id',$id)
                    ->update([
                        'status'=>'1'
                    ]);
               $status = '1';
            }
            return $status;
        }

        return false;
    }
}